<template>
  <div class="home-container">
    <!-- 导航栏组件 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        icon="search"
        round
        >搜索</van-button
      >
    </van-nav-bar>
    <van-tabs v-model="active" animated swipeable class="channel-tabs">
      <van-tab
        v-for="channel in channelsList"
        :key="channel.id"
        :title="channel.name"
      >
        <!-- 父组件将循环的数据传给子组件 -->
        <article-list :channel="channel"></article-list>
      </van-tab>
      <div slot="nav-right" class="placeholder"></div>
      <div
        slot="nav-right"
        class="hamburger-btn"
        @click="isChannelEditShow = true"
      >
        <i class="iconfont icon-gengduo"></i>
      </div>
    </van-tabs>
    <!-- 频道弹出层 -->
    <van-popup
      v-model="isChannelEditShow"
      closeable
      close-icon-position="top-left"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <channel-edit :mychannel='channelsList' :active='active' @update-active='onUpdateActive'></channel-edit>
    </van-popup>
  </div>
</template>

<script>
// 获取列表路由
import { getUserChannels } from '@/api/user'
// 列表组件
import ArticleList from './components/article-list.vue'
// 我的频道组件
import ChannelEdit from './components/channel-edit'
export default {
  name: 'Home',
  components: {
    ArticleList,
    ChannelEdit
  },
  props: {},
  data () {
    return {
      active: 0,
      channelsList: [],
      // 弹出层默认关闭
      isChannelEditShow: false
    }
  },
  created () {
    this.getUserChannels()
  },
  mounted () {},
  methods: {
    async getUserChannels () {
      try {
        const { data } = await getUserChannels()
        console.log(data)
        this.channelsList = data.data.channels
      } catch (err) {
        this.$toast('获取用户频道列表失败')
      }
    },
    onUpdateActive (index) {
      this.active = index
      this.isChannelEditShow = false
    }
  }
}
</script>

<style scoped lang="less">
.home-container {
  padding-top: 174px;
  .page-nav-bar {
    /deep/.van-nav-bar__title {
      max-width: unset;
    }
    .search-btn {
      width: 555px;
      height: 64px;
      background-color: #5babfb;
      .van-icon {
        color: #fff;
      }
    }
  }
  /deep/.channel-tabs {
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
      height: 82px;
    }
    .van-tab {
      min-width: 200px;
      border-right: 1px solid #edeff3;
      height: 80px;
      .van-tab__text {
        color: #777777;
        font-size: 27px;
      }
    }
    .van-tab--active {
      color: #333333 !important;
    }
    .van-tabs__line {
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
    }
    .van-tabs__nav {
      padding: 0;
    }
    .placeholder {
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }
    .hamburger-btn {
      position: fixed;
      right: 0;
      width: 66px;
      height: 82px;
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: #fff;
      opacity: 0.902;
      i.iconfont {
        font-size: 33px;
      }
      &:before {
        content: '';
        position: absolute;
        left: 0;
        width: 1px;
        height: 100%;
        background-image: url('~@/assets/gradient-gray-line.png');
        background-size: contain;
      }
    }
  }
}
</style>
